//
//  CustomTabBar.m
//  GetFit3.0
//
//  Created by 秀才 on 2016/11/26.
//  Copyright © 2016年 lxc. All rights reserved.
//

#import "CustomTabBar.h"
#import "SNNavigationViewController.h"
#import "SNHomePageViewController.h"
#import "SNTrackViewController.h"
#import "SNEquipmentViewController.h"
#import "SNSettingViewController.h"
#import "FindPhoneView.h"
#import <UserNotifications/UserNotifications.h>
#define SHOWTARBARVIEW 0.1
@interface CustomTabBar (){
    UIView *_tabBarView;
}
@property (nonatomic, strong) UIView         *lineView;
@property (nonatomic, strong) NSArray        *normalImageArray;
@property (nonatomic, strong) NSArray        *selectImageArray;
@property (nonatomic, strong) NSArray        *titleArray;
@property (nonatomic, strong) NSArray *vcs;
@property (nonatomic, strong) FindPhoneView  *findPhoneView;

@end

@implementation CustomTabBar
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
//    [SN_PublicClass setBLTControlTDelegateFindPhone:self];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
//    [SN_PublicClass setBLTControlTDelegateFindPhone:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
     [SN_PublicClass setBLTControlTDelegateFindPhone:self];
    //  隐藏UITabBarButton
    self.tabBar.hidden = YES;
    
    _normalImageArray = [NSArray arrayWithObjects:@"主页1", @"跑道1", @"设备1", @"设置1", nil];
    
    _selectImageArray = [NSArray arrayWithObjects:@"主页", @"跑道", @"设备", @"设置", nil];
    
    _titleArray = [NSArray arrayWithObjects:SN_Text(@"日常"), SN_Text(@"活动"), SN_Text(@"设备"), SN_Text(@"用户"), nil];
    
    SNHomePageViewController   *Vc1  = [[SNHomePageViewController alloc]init];
    SNNavigationViewController *nav1 = [[SNNavigationViewController alloc]initWithRootViewController:Vc1];
    
    SNTrackViewController      *Vc2  = [[SNTrackViewController alloc]init];
    SNNavigationViewController *nav2 = [[SNNavigationViewController alloc]initWithRootViewController:Vc2];
    
    SNEquipmentViewController  *Vc3  = [[SNEquipmentViewController alloc] init];
    SNNavigationViewController *nav3 = [[SNNavigationViewController alloc]initWithRootViewController:Vc3];
    
    SNSettingViewController    *Vc4  = [[SNSettingViewController alloc]init];
    SNNavigationViewController *nav4 = [[SNNavigationViewController alloc]initWithRootViewController:Vc4];
    
    _vcs = @[nav1, nav2, nav3, nav4];
    [self setViewControllers:_vcs animated:YES];
    
    _tabBarView                 = [[UIView alloc]initWithFrame:CGRectMake(0, HHHHEIGHT - 49, HHHWIDTH, 49)];
    _tabBarView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_tabBarView];
    
    //    UIView *line = [[UIView alloc] init];
    //    line.frame = CGRectMake(0, 0, HHHWIDTH, 3.0);
    //    line.backgroundColor = [UIColor colorFromHex:0xf3f3f3];
    //    [_tabBarView addSubview:line];
    
    SNShadowView *shadowLine = [[SNShadowView alloc] init];
    shadowLine.frame = CGRectMake(0, 0, HHHWIDTH, 3.0);
    [shadowLine showWithType:1];
    [_tabBarView addSubview:shadowLine];
    
    _tabBarView.backgroundColor = BBIGLIGHTCOLOR;
    
    for (int i = 0; i < _vcs.count; i++) {
        UIButton *tarBarButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tarBarButton.frame = CGRectMake(HHHWIDTH/_vcs.count *i, 1, HHHWIDTH/_vcs.count, 49 + FitSimpleIpx(0, 12));
        tarBarButton.tag   = i;
        [tarBarButton addTarget:self action:@selector(tabBarButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_tabBarView addSubview:tarBarButton];
        
        UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 28 +FitSimpleIpx(0, 5), 28 +FitSimpleIpx(0, 5))];
        imageV.tag    = i + 1000;
        imageV.center = CGPointMake(tarBarButton.x + HHHWIDTH/_vcs.count /2, 47/2 - FitSimpleIpx(0, 2) - 6);
        imageV.image  = [UIImage image:_normalImageArray[i]];
        [_tabBarView addSubview:imageV];
        
        UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH/_vcs.count, 20)];
        titleLabel.tag           = i + 100;
        titleLabel.font          = SNFONTMIN(12.0);
        titleLabel.textColor     = [UIColor lightGrayColor];
        titleLabel.center        = CGPointMake(tarBarButton.center.x, tarBarButton.center.y + 13 - FitSimpleIpx(0, 6));
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text          = _titleArray[i];
        titleLabel.textColor     = SNCOLORGRAY;
        [_tabBarView addSubview:titleLabel];
        
        if (i == 0) {
            titleLabel.textColor = SNCOLORBLACK;
            imageV.image         = [UIImage image:_selectImageArray[i]];
        }
    }
    
    _lineView                 = [[UIView alloc] init];
    _lineView.frame           = CGRectMake(0, 47 + FitSimpleIpx(0, 12), HHHWIDTH/_vcs.count, 2);
    _lineView.backgroundColor = [UIColor colorFromHex:0x6d6d6d];
    [_tabBarView addSubview:_lineView];
    KK_ShareData.currentVCTag = 0;
}

//  颜色转换为背景图片
- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

- (void)tabBarButtonClick:(UIButton *)sender {
    KK_ShareData.lastVcTag    = KK_ShareData.currentVCTag;
    KK_ShareData.currentVCTag = sender.tag;
    //    NSLog(@"从>>>%d 到>>>%d",KK_ShareData.lastVcTag,KK_ShareData.currentVCTag);
    
    self.selectedIndex = sender.tag;
    [UIView animateWithDuration:VIEWANIMATETIME animations:^{
        _lineView.frame = CGRectMake(HHHWIDTH/_vcs.count*sender.tag, 47 +FitSimpleIpx(0, 12), HHHWIDTH/_vcs.count, 2);
    }];
    
    [UIView animateWithDuration:0.25 animations:^{
        sender.backgroundColor = SNCOLORGRAYLINE;
    } completion:^(BOOL finished) {
        sender.backgroundColor = [UIColor clearColor];
    }];
    
    for (UILabel *view in _tabBarView.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            if (view.tag == sender.tag + 100) {
                view.textColor = SNCOLORBLACK;
            } else {
                view.textColor = SNCOLORGRAY;
            }
        }
    }
    
    for (UIImageView *view in _tabBarView.subviews) {
        if ([view isKindOfClass:[UIImageView class]]) {
            if (view.tag == sender.tag + 1000) {
                view.image = [UIImage image:_selectImageArray[view.tag - 1000]];
            } else {
                view.image = [UIImage image:_normalImageArray[view.tag - 1000]];
            }
            [view setTintColor:SNCOLORGRAY];
        }
    }
}

- (void)bleControlFindPhone {
    if (_findPhoneView) {
        [_findPhoneView stop];
        [_findPhoneView removeFromSuperview];
        _findPhoneView = nil;
        [[UIApplication sharedApplication].keyWindow.rootViewController.view willRemoveSubview:_findPhoneView];
    }
    _findPhoneView = [[FindPhoneView alloc]initWithFrame:CGRectMake(0, 0, HHHWIDTH, HHHHEIGHT)];
    [[UIApplication sharedApplication].keyWindow.rootViewController.view addSubview:_findPhoneView];
    [_findPhoneView animationUp];
    if (IOS10_OR_LATER) {//ios 10
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
        content.title = [NSString localizedUserNotificationStringForKey:SN_Text(@"找手机") arguments:nil];
        content.sound = [UNNotificationSound defaultSound];
        //content.badge = [NSNumber numberWithInteger:1];
        UNTimeIntervalNotificationTrigger *trigger    = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:1 repeats:NO];
        NSString                          *identifier = @"findPhone";
        UNNotificationRequest             *request    = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
        [center addNotificationRequest:request withCompletionHandler:nil];
    } else {
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        notification.timeZone       = [NSTimeZone localTimeZone];
        notification.fireDate       = [NSDate dateWithTimeIntervalSinceNow:3];
        notification.repeatCalendar = [NSCalendar currentCalendar];
        notification.alertBody      = SN_Text(@"找手机");
        notification.alertAction    = @"打开应用";
        notification.soundName      = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
}

- (void)showTheTabBarView {
    [UIView animateWithDuration:SHOWTARBARVIEW animations:^{
        _tabBarView.frame = CGRectMake(0, HHHHEIGHT - 49 - FitSimpleIpx(0, 12), HHHWIDTH, 49);
    }];
    //    _tabBarView.hidden = NO;
}

- (void)hiddenTheTabBarView {
    [UIView animateWithDuration:SHOWTARBARVIEW animations:^{
        _tabBarView.frame = CGRectMake(0, HHHHEIGHT, HHHWIDTH, 49);
    }];
    //    _tabBarView.hidden = YES;
}

@end
